package com.dianrui.fmi.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.dianrui.fmi.R;
import com.dianrui.fmi.bean.HelpList;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class HelpListAdapter extends BaseAdapter {

    private List<HelpList> list;
    private Context context;
    HelpClickAdapter helpClickAdapter;


    public HelpListAdapter(Context context, List<HelpList> list) {
        this.list = list;
        this.context = context;

    }

    public void refresh(List<HelpList> list) {
        this.list = list;
        this.notifyDataSetChanged();
    }


    @Override
    public int getCount() {
        if (list == null) {
            return 0;
        }
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public interface HelpClickAdapter {
        void onClick(String explainid);

    }

    public void setOnHelpClickListener(HelpClickAdapter helpClickListener) {
        this.helpClickAdapter = helpClickListener;
    }


    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView != null) {
            holder = (ViewHolder) convertView.getTag();
        } else {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_help_list, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }
        if (null != list && list.size() > 0) {
            final HelpList helpList = list.get(position);
            holder.helpName.setText(helpList.title);
            holder.help_layout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    helpClickAdapter.onClick(helpList.explain_id);

                }
            });
        }
        return convertView;
    }


    class ViewHolder {

        @BindView(R.id.help_name)
        TextView helpName;
        @BindView(R.id.help_layout)
        LinearLayout help_layout;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
